package com.lin.service.impl;

import com.lin.common.exception.BaseException;
import com.lin.entity.DynamicEntity;
import com.lin.entity.User;
import com.lin.entity.dto.UserDto;
import com.lin.entity.vo.UserVo;
import com.lin.mapper.DynamicMapper;
import com.lin.mapper.UserMapper;
import com.lin.service.PersonService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PersonServiceImpl implements PersonService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DynamicMapper dynamicMapper;
    /**
     * 获得用户数据
     * @param userDto
     * @return
     */
    public UserVo getDate(UserDto userDto) {
        User user = userMapper.findByEmailOrPhoneOrUsername(userDto);
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user,userVo);
        return userVo;
    }

    /**
     *  修改用户信息
     * @param user
     */
    public void update(User user) {
        userMapper.update(user);
    }
    /**
     * 前端获取用户发布动态信息
     *
     * @param userId
     */
    @Override
    public List<DynamicEntity> get(Integer userId) {
        return dynamicMapper.getdynamic(userId);
    }
    /**
     * 前端获取用户评论
     *
     * @param userId
     */
    @Override
    public User getUserByUserid(Integer userId){
        return userMapper.findByUserId(userId);
    }

    /**
     * 关注某人
     * @param fanId
     * @param followedId
     * @return
     */
    @Override
    public int followAUser(Integer fanId, Integer followedId) {
        User userFan = userMapper.findByUserId(fanId);
        User userFollowed = userMapper.findByUserId(followedId);
        if(userFan == null || userFollowed == null){
            throw new BaseException("互相关注用户不存在");
        }
        return userMapper.followAUser(fanId,followedId);
    }

    /**
     * 取消关注某人
     * @param fanId
     * @param followedId
     * @return
     */
    @Override
    public int cancelFollowAUser(Integer fanId, Integer followedId) {
        return userMapper.cancelFollowAUser(fanId,followedId);
    }

    /**
     * 找粉丝
     * @param userId
     * @return
     */
    @Override
    public List<User> findFan(Integer userId) {
        return userMapper.findFans(userId);
    }
    /**
     * 找关注
     * @param userId
     * @return
     */
    @Override
    public List<User> findFollow(Integer userId) {
        return userMapper.findFollows(userId);
    }

    /**
     * 查找粉丝数量
     * @param userId
     * @return
     */
    @Override
    public int getFanNumById(Integer userId){
        return userMapper.getFanNumById(userId);
    }

    /**
     * 查找关注数量
     * @param userId
     * @return
     */
    @Override
    public int getFollowNumById(Integer userId){
        return userMapper.getFollowNumById(userId);
    }

    @Override
    public int getdynamicNumById(Integer userId){
        return userMapper.getdynamicNumById(userId);
    }
}
